package atm;

import lovo01.ATM04.FileUtil;
import lovo01.ATM04.Users;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 转账界面
 */
public class TransferPanel extends JPanel implements ActionListener {
    private ATM05 atm05;
    private JLabel accountLabel = new JLabel("请输入对方账号:");
    private JTextField accountTextField = new JTextField();
    private JLabel transferLabel = new JLabel("请输入转账金额:");
    private JTextField transferTextField = new JTextField();
    private JButton sureBtn = new JButton(new ImageIcon("images/buttonSure.jpg"));
    private JButton offBtn = new JButton(new ImageIcon("images/buttonOff.jpg"));
    private Font font = new Font("宋体",Font.BOLD,25);

    public TransferPanel(ATM05 atm05){
        this.atm05 = atm05;
        this.setLayout(null);
        this.setOpaque(false);

        accountLabel.setBounds(200,200,300,50);
        accountLabel.setFont(font);
        accountTextField.setBounds(450,200,250,50);
        accountTextField.setFont(font);
        transferLabel.setBounds(200,250,300,50);
        transferLabel.setFont(font);
        transferTextField.setBounds(450,250,250,50);
        transferTextField.setFont(font);
        sureBtn.setBounds(320,320,100,50);
        offBtn.setBounds(450,320,100,50);
        sureBtn.addActionListener(this);
        offBtn.addActionListener(this);

        this.add(accountLabel);
        this.add(accountTextField);
        this.add(transferLabel);
        this.add(transferTextField);
        this.add(sureBtn);
        this.add(offBtn);
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == sureBtn){
            String otherAccount = accountTextField.getText();
            Users loginUser = atm05.getLoginUser();
            if (loginUser.getName().equals(otherAccount)){
                JOptionPane.showMessageDialog(this,"不能转给自己");
                return;
            }
            boolean flag = false;
            int otherIndex = 0;
            for (int i = 0;i < atm05.getUsers().size();i++){
                if (otherAccount.equals(atm05.getUsers().get(i).getName())){
                    flag = true;
                    otherIndex = i;
                    break;
                }
            }
            if (!flag){
                JOptionPane.showMessageDialog(this,"账号不存在");
                return;
            }
            int money = 0;
            try {
                String money02 = transferTextField.getText();
                money = Integer.parseInt(money02);
            }catch (NumberFormatException e1){
                JOptionPane.showMessageDialog(this,"输入错误,请重新输入");
                transferTextField.setText(null);
                return;
            }
            if (loginUser.getMoney() < money){
                JOptionPane.showMessageDialog(this,"余额不足");
                return;
            }
            loginUser.setMoney(loginUser.getMoney() - money);
            atm05.getUsers().get(otherIndex).setMoney(atm05.getUsers().get(otherIndex).getMoney() + money);
            FileUtil.writeObject(atm05.getUsers(),ATM05.FILE_PATH);
            JOptionPane.showMessageDialog(this,"转账成功");
            accountTextField.setText("");
            transferTextField.setText("");
            atm05.showPanel("menu");
        }
        if (e.getSource().equals(offBtn)) {
            accountTextField.setText("");
            transferTextField.setText("");
            atm05.showPanel("menu");
        }
        }
    }

